#ifndef GLVIEWPORT_H
#define GLVIEWPORT_H

#include <QObject>
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLTexture>
#include <QOpenGLBuffer>
#include <QOpenGLShaderProgram>



class GLViewport : public QOpenGLWidget, public QOpenGLFunctions
{
    Q_OBJECT
public:
    explicit GLViewport(QWidget *parent = nullptr);

public slots:
    void initializeGL() override;
    void resizeGL(int w, int h) override;
    void paintGL() override;

    void updateImage(const QImage &img);

    void updateYuv422(quint8* dataY, quint8* dataU, quint8* dataV, int width, int height);

    void initShaders();
private:
    void initTexture();

signals:

private:

//    QOpenGLTexture *m_pTexture{nullptr};
//    QOpenGLShaderProgram program;
//    QMatrix4x4 projection;
//    QVector<QVector3D> vertices;
//    QVector<QVector2D> texCoords;

    // for yuv
    QOpenGLShaderProgram *m_pProgram{nullptr};
    QOpenGLBuffer m_vbo;
    GLuint m_textureUniformY{0},m_textureUniformU{0},m_textureUniformV{0};
    QOpenGLTexture *m_pTextureY{nullptr};
    QOpenGLTexture *m_pTextureU{nullptr};
    QOpenGLTexture *m_pTextureV{nullptr};
    GLuint m_idY{0},m_idU{0},m_idV{0};

    struct Frame{quint8* dataY{nullptr}; quint8* dataU{nullptr}; quint8* dataV{nullptr}; int width{0}; int height{0};} m_currentFrame;
};

#endif // GLVIEWPORT_H
